//
//  DianPuViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "DianPuViewController.h"

#import "DPDetView.h"
#import "DPDetCell.h"
#import "BuyCarDetViewController.h"
#import "WTImageScroll.h"
#import "WashMapViewController.h"
#import "StarView.h"

#import "CarAllView.h"
#import "CarPriceView.h"
#import "CarSpecialView.h"
#import "CarKMView.h"

#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
<<<<<<< .mine

#import "HJCarDetail.h"

||||||| .r207
=======
#import "SDCycleScrollView.h"
>>>>>>> .r244
@interface DianPuViewController ()<UITableViewDelegate,UITableViewDataSource,CarKMViewDelegate,CarSpecialViewDelegate,CarPriceViewDelegate,CarAllViewDelegate>{
    NSMutableArray *datas;
    CarAllView *allView;
    CarPriceView *priceView;
    CarSpecialView *specailView;
    CarKMView *kmView;
    
    UIView *headerview;
    UIButton *btn1;
    UIButton *btn2;
    UIButton *btn3;
    UIButton *btn4;
    UIButton *btnBackTop;//回到顶部按钮
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableDictionary *dic;
@property (nonatomic,strong)NSMutableDictionary *parmas;
@end

@implementation DianPuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    datas = [NSMutableArray array];
    _dic = [NSMutableDictionary dictionary];
    [_dic setObject:@"综合" forKey:@"state"];
    [_dic setObject:@"价格" forKey:@"money"];
    [_dic setObject:@"性质" forKey:@"type"];
    [_dic setObject:@"里程" forKey:@"travel"];
    _parmas = [NSMutableDictionary dictionary];
    [_parmas setObject:@"" forKey:@"state"];
    [_parmas setObject:@"" forKey:@"money"];
    [_parmas setObject:@"" forKey:@"type"];
    [_parmas setObject:@"" forKey:@"travel"];
    
    [self showNav:@"店铺详情"];
    //NSLog(@"images:%@",_arrays);
    [self loadData];
    
}
/*
 
 */


- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    DPDetView *view = [[[NSBundle mainBundle] loadNibNamed:@"DPDetView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 280.0);
    _tableView.tableHeaderView = view;
    
    view.labName.text = _name;
    view.labPlace.text = _place;
    view.labCount.text = _count;
    view.labKm.text = _km;
    
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    CGFloat count = [_start floatValue];
    starView.showStar = count*20;
    [view.starView addSubview:starView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMap)];
    view.imageMap.userInteractionEnabled = YES;
    [view.imageMap addGestureRecognizer:tap];
    
    //轮播图
    UIView *banner = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 170) andImageArray:_arrays andBtnClick:^(NSInteger tagValue) {
        
    }];
    //banner.backgroundColor = [UIColor redColor];
    [view.viewBanner addSubview:banner];
    
    
    btnBackTop = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBackTop.hidden = YES;
    btnBackTop.frame = CGRectMake(ScreenWidth-16-49, ScreenHeight-49-20, 49, 49);
    [btnBackTop setImage:[UIImage imageNamed:@"返回顶部"] forState:UIControlStateNormal];
    [btnBackTop setImage:[UIImage imageNamed:@"返回顶部"] forState:UIControlStateHighlighted];
    [btnBackTop addTarget:self action:@selector(clickTop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBackTop];
    
}
//http://blog.csdn.net/alexander_wei/article/details/50809228 导航
- (void)clickMap{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"丫丢丢想访问您的地图" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
            //MKMapItem 使用场景: 1. 跳转原生地图 2.计算线路
            MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
            
            //地理编码器
            CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            //我们假定一个终点坐标，上海嘉定伊宁路2000号报名大厅:121.229296,31.336956
            [geocoder geocodeAddressString:_place completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                CLPlacemark *endPlacemark  = placemarks.lastObject;
                
                //创建一个地图的地标对象
                MKPlacemark *endMKPlacemark = [[MKPlacemark alloc] initWithPlacemark:endPlacemark];
                //在地图上标注一个点(终点)
                MKMapItem *endMapItem = [[MKMapItem alloc] initWithPlacemark:endMKPlacemark];
                
                //MKLaunchOptionsDirectionsModeKey 指定导航模式
                //NSString * const MKLaunchOptionsDirectionsModeDriving; 驾车
                //NSString * const MKLaunchOptionsDirectionsModeWalking; 步行
                //NSString * const MKLaunchOptionsDirectionsModeTransit; 公交
                [MKMapItem openMapsWithItems:@[currentLocation, endMapItem]
                               launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
                
            }];
        }
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)clickTop{
    [UIView animateWithDuration:0.3 animations:^{
        [_tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    }];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>200) {
        btnBackTop.hidden = NO;
    }else{
        btnBackTop.hidden = YES;
    }
}


#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    DPDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DPDetCell" owner:nil options:nil] lastObject];
    }
    NSDictionary *dic = datas[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lab2.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
    cell.lab2.layer.cornerRadius = 5.0;
    cell.lab2.layer.borderWidth = 1.0;
    
    cell.lab1.text = [dic objectForKey:@"brand"];
    NSString *type = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"type"] integerValue]];
    if ([type isEqualToString:@"1"]) {
        cell.lab2.text = @"准新车";
    }else{
        cell.lab2.text = @"新车";
    }
    //
    cell.lab3.text = [NSString stringWithFormat:@"%@万",[dic objectForKey:@"guideprice"]];
    cell.lab4.text = [NSString stringWithFormat:@"%@万",[dic objectForKey:@"sellingprice"]];
    //浏览量
    NSString *count = [NSString stringWithFormat:@" %li",[[dic objectForKey:@"see"] integerValue]];
    NSInteger a = [count integerValue];
    if ([count integerValue] >= 10000) {
        CGFloat n = a /10000.0;
        NSString *str = [NSString stringWithFormat:@"%.1f万",n];
        [cell.btnLL setTitle:str forState:UIControlStateNormal];
    }else{
        [cell.btnLL setTitle:count forState:UIControlStateNormal];
    }
    
    
    [cell.btnLL setTitleEdgeInsets:UIEdgeInsetsMake(0, -cell.btnLL.imageView.frame.size.width, 0, cell.btnLL.imageView.frame.size.width)];
    [cell.btnLL setImageEdgeInsets:UIEdgeInsetsMake(0, cell.btnLL.titleLabel.frame.size.width+5, 0, -cell.btnLL.titleLabel.frame.size.width)];
    
    NSString *imgurl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"carthemephoto"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"timg22"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 116.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = datas[indexPath.row];
    NSString *carid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    BuyCarDetViewController *det = [[BuyCarDetViewController alloc] init];
    det.carId = carid;
    det.lat = _lat;
    det.lng = _lng;
    [self.navigationController pushViewController:det animated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    headerview.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    lab.backgroundColor = [UIColor grayColor];
    [headerview addSubview:lab];
    //
    btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, ScreenWidth/4, 40);
    [btn1 setTitle:[_dic objectForKey:@"state"] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn1.imageView.image.size.width, 0, btn1.imageView.image.size.width)];
    [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0, btn1.titleLabel.frame.size.width+5, 0, -btn1.titleLabel.frame.size.width)];
    [btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
    [headerview addSubview:btn1];
    
    btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth/4, 0, ScreenWidth/4, 40);
    [btn2 setTitle:[_dic objectForKey:@"money"] forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn2.imageView.image.size.width, 0, btn2.imageView.image.size.width)];
    [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0, btn2.titleLabel.frame.size.width+5, 0, -btn2.titleLabel.frame.size.width)];
    [btn2 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
    [headerview addSubview:btn2];
    
    btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(ScreenWidth/2, 0, ScreenWidth/4, 40);
    [btn3 setTitle:[_dic objectForKey:@"type"] forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn3 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn3.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn3 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn3.imageView.image.size.width, 0, btn3.imageView.image.size.width)];
    [btn3 setImageEdgeInsets:UIEdgeInsetsMake(0, btn3.titleLabel.frame.size.width+5, 0, -btn3.titleLabel.frame.size.width)];
    [btn3 addTarget:self action:@selector(click3) forControlEvents:UIControlEventTouchUpInside];
    [headerview addSubview:btn3];
    
    btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame = CGRectMake(ScreenWidth/4*3, 0, ScreenWidth/4, 40);
    [btn4 setTitle:[_dic objectForKey:@"travel"] forState:UIControlStateNormal];
    [btn4 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn4 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn4.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn4 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn4.imageView.image.size.width, 0, btn4.imageView.image.size.width)];
    [btn4 setImageEdgeInsets:UIEdgeInsetsMake(0, btn4.titleLabel.frame.size.width+5, 0, -btn4.titleLabel.frame.size.width)];
    [btn4 addTarget:self action:@selector(click4) forControlEvents:UIControlEventTouchUpInside];
    [headerview addSubview:btn4];
    
    return headerview;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}
//综合搜索
- (void)click1{
    //[_tableView setContentOffset:CGPointMake(0, 280) animated:YES];
    if (![allView superview]) {
        allView = [[CarAllView alloc] init];
        allView.delegate = self;
        [self.view addSubview:allView];
        [allView show];
    }
    [priceView removeFromSuperview];
    [specailView removeFromSuperview];
    [kmView removeFromSuperview];
}
//价格搜索
- (void)click2{
//    [_tableView setContentOffset:CGPointMake(0, 280) animated:YES];
    
    if (![priceView superview]) {
        priceView = [[CarPriceView alloc] init];
        priceView.delegate = self;
        [self.view addSubview:priceView];
        [priceView show];
    }
    [allView removeFromSuperview];
    [specailView removeFromSuperview];
    [kmView removeFromSuperview];
}
//性质搜索
- (void)click3{
//    [_tableView setContentOffset:CGPointMake(0, 280) animated:YES];
    if (![specailView superview]) {
        specailView = [[CarSpecialView alloc] init];
        specailView.delegate = self;
        [self.view addSubview:specailView];
        [specailView show];
    }
    [priceView removeFromSuperview];
    [allView removeFromSuperview];
    [kmView removeFromSuperview];
}
//里程搜索
- (void)click4{
//    [_tableView setContentOffset:CGPointMake(0, 280) animated:YES];
    if (![kmView superview]) {
        kmView = [[CarKMView alloc] init];
        kmView.delegate = self;
        [self.view addSubview:kmView];
        [kmView show];
    }
    [priceView removeFromSuperview];
    [specailView removeFromSuperview];
    [allView removeFromSuperview];
}

-(void)sendAll:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"浏览最多"]) {
        [_parmas setObject:@"2" forKey:@"state"];
    }else if ([text isEqualToString:@"出厂时间最短"]){
        [_parmas setObject:@"3" forKey:@"state"];
    }else{
        [_parmas setObject:@"" forKey:@"state"];
    }
    [_dic setObject:text forKey:@"state"];
    [self loadData];
}

-(void)sendPrice:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"价格由高到低"]) {
        [_parmas setObject:@"2" forKey:@"money"];
    }else if ([text isEqualToString:@"价格由低到高"]){
        [_parmas setObject:@"3" forKey:@"money"];
    }else{
        [_parmas setObject:@"" forKey:@"money"];
    }
    [_dic setObject:text forKey:@"money"];
    [self loadData];
}
//
-(void)sendSpecial:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"准新车"]) {
        [_parmas setObject:@"2" forKey:@"type"];
    }else if ([text isEqualToString:@"新车"]){
        [_parmas setObject:@"3" forKey:@"type"];
    }else{
        [_parmas setObject:@"" forKey:@"type"];
    }
    
    [_dic setObject:text forKey:@"type"];
    [self loadData];
}

-(void)sendKM:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"10公里内"]) {
        [_parmas setObject:@"10" forKey:@"travel"];
    }else if ([text isEqualToString:@"50公里内"]){
        [_parmas setObject:@"50" forKey:@"travel"];
    }else if ([text isEqualToString:@"100公里内"]){
        [_parmas setObject:@"100" forKey:@"travel"];
    }else if ([text isEqualToString:@"100公里外"]){
        [_parmas setObject:@"101" forKey:@"travel"];
    }else{
        [_parmas setObject:@"" forKey:@"travel"];
    }
    [_dic setObject:text forKey:@"travel"];
    [self loadData];
}

//店铺详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:_CarId forKey:@"id"];
    
    NSString *state = [_parmas objectForKey:@"state"];
    NSString *money = [_parmas objectForKey:@"money"];
    NSString *travel = [_parmas objectForKey:@"travel"];
    NSString *type = [_parmas objectForKey:@"type"];
    if (state.length > 0) {
        [parmas setObject:state forKey:@"state"];
    }
    
    if (money.length > 0){
        [parmas setObject:money forKey:@"money"];
    }
    
    if (travel.length > 0){
        [parmas setObject:travel forKey:@"travel"];
    }
    
    if (type.length > 0){
        [parmas setObject:type forKey:@"type"];
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
<<<<<<< .mine
    [manager POST:@"http://www.diudiujituan.com/index.php/App/CarBuying/carbuying" parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
||||||| .r207
    [manager POST:@"http://ydd.xtangguo.com/index.php/App/CarBuying/carbuying" parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
=======
    [manager POST:[NSString stringWithFormat:@"%@%@",urlHead,@"/App/CarBuying/carbuying"] parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
>>>>>>> .r244
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([state integerValue] == 200) {
            datas = [responseObject objectForKey:@"result"];
//            NSArray * arr = [HJCarDetail mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
//            [datas removeAllObjects];
//            [datas addObjectsFromArray:arr];
        }
        
        [_tableView reloadData];
        //
        [self loadUI];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}



@end
